var express = require('express');
var request=require('request');
var proxy = require('http-proxy-middleware');
const webpack = require('webpack');
const config = require('./webpack.config');
const bodyParser = require('body-parser');
const path = require('path');
var app = express();


const isProduction = process.env.NODE_ENV === 'production';
const isDeveloping = !isProduction;

//  RESTful API
/*const publicPath = path.resolve(__dirname,'build');
 app.use(bodyParser.json({ type: 'application/json' }))
 app.use(express.static(publicPath));*/

var allowCrossDomain = function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,POST');
  return next();
}

app.use(allowCrossDomain);

// Webpack developer
if (isDeveloping) {
  const compiler = webpack(config);
  app.use(require('webpack-dev-middleware')(compiler, {
    publicPath: config.output.publicPath,
    hot: true,
    inline: true,
    historyApiFallback: true
  }));

  app.use(require('webpack-hot-middleware')(compiler));
}

var hostProxy = proxy({
  //target: 'http://10.10.1.27:8080',
   target: 'http://111.231.64.195:10040',
  // target: 'http://10.10.2.178:8081',
     //target: 'http://www.izupai.com',
 // target: 'http://web-azp.zijincunguan.erongyun.net',
  changeOrigin: true,
  logLevel: 'debug'
});

// 通常用于加载静态资源
app.use(express.static(__dirname + '/build'))


app.get('*', function (req, res) {
  console.log(req.url);
  res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});

app.use('*', hostProxy);

var server = app.listen(9090, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', 'localhost', port);
});
